<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label-width="40" label="ID" prop="id">
        <el-input
          type="number"
          v-model="queryParams.id"
          placeholder="ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标号名称" prop="name">
        <el-input
          v-model.trim="queryParams.name"
          placeholder="请输入标号名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:title:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="titleList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ backgroundColor: '#F7F8FA' }"
      height="420"
    >
      <el-table-column
        fixed="left"
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column width="55" label="ID" align="center" prop="id" />
      <el-table-column
        label="附加标号名称"
        align="center"
        prop="name"
        show-overflow-tooltip
      />
      <el-table-column label="基准价格" align="center" prop="price" />
      <el-table-column label="最后编辑时间" align="center" prop="updateTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            :value="scope.row.status == '0'"
            @change="changeStatus(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        width="160px"
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:title:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:title:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品-附加标号对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item
          :rules="[
            { required: true, message: '请填写附件标号名称', trigger: 'blur' },
          ]"
          label="附件标号名称"
          prop="name"
        >
          <el-input v-model="form.name" placeholder="请输入附件标号名称" />
        </el-form-item>
        <NumberFormItem
          requiredMessage="请填写基准价格"
          errorTip="基准价格不能为负数"
          label="基准价格(元)"
          prop="price"
        >
          <el-input
            type="number"
            v-model="form.price"
            placeholder="请填写基准价格"
          />
        </NumberFormItem>
        <el-form-item
          :rules="[
            { required: true, message: '请选择状态', trigger: 'change' },
          ]"
          label="状态"
          prop="status"
        >
          <el-switch
            :value="form.status == '0'"
            @change="
              (value) => {
                form.status = value ? '0' : '1';
              }
            "
          ></el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTitle,
  getTitle,
  delTitle,
  addTitle,
  updateTitle,
} from "@/api/service/submain";
import NumberFormItem from "@/components/NumberFormItem";
export default {
  components: {
    NumberFormItem,
  },
  name: "Title",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品-附加标号表格数据
      titleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        id: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 开关
    changeStatus(item) {
      const form = { ...item };

      // 改变状态
      form.status = form.status == "0" ? 1 : 0;

      updateTitle(form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        item.status = form.status;
        this.getList();
      });
    },
    /** 查询产品-附加标号列表 */
    getList() {
      this.loading = true;
      listTitle(this.queryParams).then((response) => {
        this.titleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        price: null,
        status: 0,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增附加标号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTitle(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改附加标号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTitle(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.resetQuery();
            });
          } else {
            addTitle(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.resetQuery();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除产品-附加标号编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTitle(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/title/export",
        {
          ...this.queryParams,
        },
        `title_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
